package com.info.zhiduoduo.core.service.payment.project;

import com.info.zhiduoduo.common.configs.CommonConfig;
import com.info.zhiduoduo.common.configs.KltPayConfig;
import com.info.zhiduoduo.common.configs.PayConfig;
import com.info.zhiduoduo.common.configs.ZddUrlConfig;
import com.info.zhiduoduo.common.constants.Constants;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.project.CompanyProjectState;
import com.info.zhiduoduo.common.exception.SmartException;
import com.info.zhiduoduo.common.utils.RandomUtil;
import com.info.zhiduoduo.core.dto.project.CompanyCheckProjectOfflineDto;
import com.info.zhiduoduo.core.service.CoreCompanyService;
import com.info.zhiduoduo.core.service.CoreProjectService;
import com.info.zhiduoduo.core.service.DingdingNoticeService;
import com.info.zhiduoduo.core.service.WxCrowdsourceMessageService;
import com.info.zhiduoduo.core.service.WxMessageService;
import com.info.zhiduoduo.core.service.WxPartnerMessageService;
import com.info.zhiduoduo.core.service.ZhiduoduoProjectService;
import com.info.zhiduoduo.core.service.impl.CeoNoticeService;
import com.info.zhiduoduo.repository.dto.maker.MakerDetailDto;
import com.info.zhiduoduo.repository.entity.company.CompanyProject;
import com.info.zhiduoduo.repository.entity.invoice.Invoice;
import com.info.zhiduoduo.repository.mapper.company.CompanyProjectMapper;
import com.info.zhiduoduo.repository.mapper.crowdsource.CrowdsourceAccountMapper;
import com.info.zhiduoduo.repository.mapper.crowdsource.CrowdsourceProjectItemMapper;
import com.info.zhiduoduo.repository.mapper.crowdsource.CrowdsourceProjectMapper;
import com.info.zhiduoduo.repository.mapper.maker.MakerMapper;
import com.info.zhiduoduo.repository.mapper.maker.MakerStudioMapper;
import com.info.zhiduoduo.repository.mapper.partner.PartnerBrokerageMapper;
import com.info.zhiduoduo.repository.mapper.partner.PartnerCompanyMapper;
import com.info.zhiduoduo.repository.mapper.partner.PartnerMapper;
import com.info.zhiduoduo.repository.mapper.partner.PartnerSubMapper;
import com.info.zhiduoduo.repository.mapper.system.ProjectCategoryMapper;
import com.info.zhiduoduo.repository.mapper.taxer.TaxerInvoiceMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

import lombok.extern.slf4j.Slf4j;

/**
 * 企业使用线下方式支付项目款的业务逻辑
 */
@Slf4j
@Service
@Transactional
public class ProjectOfflinePaymentServiceImpl extends BaseProjectPaymentService {

	@Autowired
	MakerMapper makerMapper;
	@Autowired
	PartnerCompanyMapper partnerCompanyMapper;
	@Autowired
	PartnerBrokerageMapper partnerBrokerageMapper;
	@Autowired
	ProjectCategoryMapper projectCategoryMapper;
	@Autowired
	CompanyProjectMapper projectMapper;
	@Autowired
	CrowdsourceAccountMapper crowdsourceAccountMapper;
	@Autowired
	CrowdsourceProjectItemMapper crowdsourceProjectItemMapper;
	@Autowired
	CrowdsourceProjectMapper crowdsourceProjectMapper;
	@Autowired
	MakerStudioMapper makerStudioMapper;
	@Autowired
	TaxerInvoiceMapper taxerInvoiceMapper;
	@Autowired
	PartnerSubMapper partnerSubMapper;
	@Autowired
	PartnerMapper partnerMapper;

	@Autowired
	CoreCompanyService coreCompanyService;
	@Autowired
	DingdingNoticeService dingdingNoticeService;
	@Autowired
	WxMessageService wxMessageService;
	@Autowired
	WxPartnerMessageService wxPartnerMessageService;
	@Autowired
	WxCrowdsourceMessageService wxCrowdsourceMessageService;
	@Autowired
	CeoNoticeService ceoNoticeService;
	@Autowired
	CoreProjectService coreProjectService;
	@Autowired
	ZhiduoduoProjectService zhiduoduoProjectService;

	@Autowired
	CommonConfig commonConfig;
	@Autowired
	ZddUrlConfig zddUrlConfig;
	@Autowired
	PayConfig payConfig;
	@Autowired
	KltPayConfig kltPayConfig;

	@Override
	public SmartResult handlePayment(CompanyProject project) {
		//直接修改项目状态为已完成,仅返回创客账号信息,让企业线下付款到创客的卡上.
		boolean update = project.setState(CompanyProjectState.COMPLETED)
				.setProjectfinish(new Date())
				.setOfflinePayState(Constants.OFFLINE_PAYING)
				.updateById();
		if (!update) throw SmartException.of();

		// 创建发票
		Invoice invoice = projectMapper.getPendingInvoiceDetail(project.getProjectid());
		if (invoice == null) return SmartResult.ofErrorParam();

		// 应付金额
		BigDecimal payAmount = project.getProjectservicecharge().add(project.getProjecttaxfee());
		String applyno = RandomUtil.createRandomNo("INVO_", "");
		invoice.setApplyno(applyno)
				.setUserid(project.getUserid())
				.setInvoiceAmount(project.getProjectdealamount())
				.setPayAmount(payAmount)
				.setState(Constants.invoicestate_2)
				.setProjectfinishtime(new Date());

		if (!invoice.insert()) throw SmartException.of("发票创建失败");

		MakerDetailDto maker = makerMapper.getUserDetail(project.getUserid());
		String bankname = maker.getBankname();
		String acctno = maker.getAcctno();
		String realname = maker.getRealname();
		CompanyCheckProjectOfflineDto offlineDto = new CompanyCheckProjectOfflineDto().setBankname(bankname)
				.setAcctNo(acctno)
				.setRealname(realname)
				.setTaxFee(project.getProjecttaxfee())
				.setServiceCharge(project.getProjectservicecharge())
				.setTotalCharge(project.getProjectservicecharge().add(project.getProjecttaxfee()));
		return SmartResult.success(offlineDto);
	}

}
